function shadowBoxTipPrompt( p ) {

    Shadowbox.open( { content: '',
                      player: "html",
                      title: "Tip Without Label",
                      height: $(window).height() * .50,
                      width: $(window).width() * .75,
                      options: {
                          onFinish: function() { addLabellessTipPrompt( p ); } } } );

}

function getAjaxPrune( event ) { ajaxPrune( event.data ); }
function getEditLabelDialogue( event ) { setTimeout( showEditLabel, 500, event.data ); }
function closeShadowbox() { Shadowbox.close(); }

function addLabellessTipPrompt( p ) {
    
    var treeId = globals.treeId;

    $('#sb-player').append(
        makeEl('div').attr( { 'class': 'topMargin1' } ).append(
            makeEl('div').attr( { 'class': 'center' } ).text('You are pruning your tree such that you will be left with a tip without a label. If you continue, you will be unable to import a Newick formatted string for this tree.  Please make a selection.'),
            makeEl('div').attr( { 'class': 'topMargin1 center' } ).append(
                makeEl('a').attr( { 'class': 'padding10p', href: 'javascript:void(0);' } )
                           .text('Continue Anyway')
                           .click( closeShadowbox )
                           .bind( 'click', { treeId: treeId, nodeIds: p.nodeIds }, getAjaxPrune ) ),
            makeEl('div').attr( { 'class': 'topMargin1 center' } ).append(
                makeEl('a').attr( { 'class': 'padding10p', href: 'javascript:void(0);' } )
                           .text('Add Label to Tip')
                           .bind( 'click', { id: p.id, postPrune: { treeId: treeId, nodeIds: p.nodeIds } }, getEditLabelDialogue )
                           .click( closeShadowbox ) ),
            makeEl('div').attr( { 'class': 'topMargin1 center' } ).append(
                makeEl('a').attr( { 'class': 'padding10p', href: 'javascript:void(0);' } )
                           .text('Cancel Prune')
                           .click( closeShadowbox ) ) ) );
}

function showSupportValueOptions() {

    var myWindow = $(window);

    Shadowbox.open( { content: "",
                      player: "html",
                      title: "Support Value Options",
                      height: myWindow.height() * .5,
                      width: myWindow.width() * .5,
                      options: { onFinish: addSupportValueContent } } );
}

function showOtherOptions() {

    var myWindow = $(window);
        
    Shadowbox.open( { content: "",
                      player: "html",
                      title: "Display Options",
                      height: myWindow.height() * .75,
                      width: myWindow.width() * .75,
                      options: { onFinish: addConfigContent } } );
}

function addSupportValueContent() {

    var localGlobal = globals;

    var suggestedThreshold = localGlobal.supportValueInfo.threshold;
   
    var betweenZeroAndOne;
    
    if( suggestedThreshold == undefined ) {
    
        var labels = localGlobal.labelInfo.labels;
    
        for( var id in labels ) {

            var label = labels[id];

            if( labels.isInternal ) {

                var numeric = parseInt( label.attr('text') );

                if( numeric >= 0 && numeric <= 1 ) {

                    betweenZeroAndOne = true;

                } else {

                    betweenZeroAndOne = false;
                }

                break;
            }
        }

        if( betweenZeroAndOne ) { suggestedThreshold = .7; } else { suggestedThreshold = 70; }

    }
    
    $('#sb-player').append(
        makeEl('div').attr( { 'class': 'topMargin1' } ).append(
            makeEl('div').attr( { 'class': 'center' } ).text('This option allows you to specify a support value threshold.  Any value above the threshold will thicken the clade.'),
            makeEl('div').attr( { 'class': 'topMargin2 paddingBottom5' } ).append(
                makeEl('div').attr( { 'class': 'fLeft leftMargin1 width30' } ).text('Threshold : '),
                makeEl('div').attr( { 'class': 'fLeft' } ).append(
                    makeEl('input').attr( { size: '10', 'class': 'right', id: 'supportValueThreshold' } ).val( suggestedThreshold ) ),
                makeEl('div').attr( { 'class': 'clear' } ) ),
            
            makeEl('div').attr( { 'class': '' } ).append(
                makeEl('div').attr( { 'class': 'fLeft leftMargin1 width30' } ).text('Thicken clade by : '),
                makeEl('div').attr( { 'class': 'fLeft' } ).append(
                    makeEl('input').attr( { size: '10', 'class': 'right', id: 'thicknessToAdd' } ).val( 6 ),
                    makeEl('span').attr( { 'class': 'smallText leftMargin1' } ).text('Current clade thickness : ' + config.pathWidth.value ) ),
                makeEl('div').attr( { 'class': 'clear' } ) ),

            makeEl('div').attr( { 'class': 'center topMargin2' } ).append(
                makeEl('div').attr( { 'class': 'width33 fLeft' } ).append(
                    makeEl('a').attr( { 'id': 'shadowboxSubmitButton', 'href': 'javascript:void(0);' } )
                               .text('Annotate Tree')
                               .bind( 'click', ajaxThickenSupportValueClades )                                                 
                               .bind( 'click', closeShadowbox ) ),
                makeEl('div').attr( { 'class': 'width33 fLeft' } ).append(
                    makeEl('a').attr( { 'href': 'javascript:void(0);' } ).text('Cancel').click( closeShadowbox ) ),
                makeEl('div').attr( { 'class': 'width33 fLeft' } ).append(
                    makeEl('a').attr( { 'href': 'javascript:void(0);' } )
                               .text('Remove Current Annotation')
                               .click( ajaxRemoveSupportValueAnnotation )
                               .click( closeShadowbox ) ),
                makeEl('div').attr( { 'class': 'clear' } ) ) ) );

    $('#supportValueThreshold').focus();
}

function addConfigContent() {

    $('#sb-player').append(
        makeEl('div').attr( { 'class': 'width50 fLeft', 'id': 'configLeftCol' } ),
        makeEl('div').attr( { 'class': 'width50 fLeft', 'id': 'configRightCol' } ),
        makeEl('div').attr( { 'class': 'clear' } ) );

    var side = 'Left';
    for( var item in config ) {
        $('#config' + side + 'Col').append(
            makeEl('div').attr( { 'class': 'padding1' } ).append(
                makeEl('div').attr( { 'class': 'right fLeft width40 rightMargin1' } ).text( config[item].string + ' : ' ),
                makeEl('div').attr( { 'class': 'fLeft width40' } ).append(
                    makeEl('input').attr( { 'id': item } ).val( config[item].value ) ),
                makeEl('div').attr( { 'class': 'clear' } ) ) );

        side = ( side == 'Left' ) ? 'Right' : 'Left';
    }

    $('#sb-player').append(
        makeEl('div').attr( { 'class': 'right width90' } ).append(
            makeEl('a').attr( { 'id': 'shadowboxSubmitButton', 'class': 'rightMargin1', 'href': 'javascript:void(0);' } ).text('Save').click( function() { saveConfig(); } ),
            makeEl('a').attr( { 'href': 'javascript:void(0);' } ).text('Cancel').click( function() { Shadowbox.close(); } ) ) );

    $('#configLeftCol input')[0].focus();
}

function showEditLabel( p ) {

    Shadowbox.open( { content: "",
                      player: "html",
                      title: ( p.text ) ? "Edit Label" : "Add Label",
                      height: 100,
                      width: 300,
                      options: {
                          onFinish: function() { showEditLabelContent( p ); } } } );
}

function showEditLabelContent( p ) {

    $('#sb-player').append(
        makeEl('div').attr( { 'class': 'topMargin1' } ).append(
            makeEl('div').attr( { 'class': 'width20 right fLeft rightMargin1' } ).text( 'Name : ' ),
            makeEl('div').attr( { 'class': 'width70 fLeft' } ).append(
                makeEl('input').attr( { 'id': 'editLabel',
                                        'value': ( p.text ) ? p.text : '' } ) ),
            makeEl('div').attr( { 'class': 'clear' } ) ),
            makeEl('div').attr( { 'class': 'right width90' } ).append(
                makeEl('a').attr( { 'id': 'shadowboxSubmitButton', 
                                    'class': 'rightMargin1',
                                    'href': 'javascript:void(0);' } )
                           .text('Save')
                           .bind( 'click', p, getEditLabel )
                           .click( closeShadowbox ),
                makeEl('a').attr( { 'href': 'javascript:void(0);' } )
                           .text('Cancel')
                           .click( closeShadowbox ) ) );

    $('#editLabel').focus();
             
}

function getEditLabel( event ) { editLabel( event.data ); }

function editLabel( p ) {

    var localGlobal = globals;

    var newLabel = $.trim( $('#editLabel').val() );

    if( (!p.text) && newLabel == '' ) { return; }

    closeShadowbox();

    if( p.label && (newLabel == p.text) ) { return; }

    var successFunction =
        ( p.postPrune )
            ? function() { ajaxPrune( p.postPrune ) }
            : function() { updateLabel( { id: p.id, value: newLabel } ); };

    $.ajax( { url: '/' + localGlobal.applicationName + '/default/editLabel',
              type: "POST",
              data: { id: p.id, label: newLabel, treeId: localGlobal.treeId },
              success: successFunction } );
}

function saveConfig() {

    var changed = { };
    var somethingChanged = false;
    var maxTipsUpdated = false;

    for( var item in config ) {

        var formValue = $.trim( $('#' + item).val() );

        if( formValue.match(/^\d+$/) ) { formValue = formValue * 1; }

        if( config[item].value != formValue ) {

            config[item].value = formValue;
            
            if( item == 'maxTips' ) { maxTipsUpdated = true; }
            else { config[item].onchange( formValue ); }

            changed[item] = formValue;
            somethingChanged = true;
        }
    }

    if( somethingChanged ) {

        var successHandler = ( maxTipsUpdated ) ? function() { config.maxTips.onchange( config.maxTips.value ); } : function() { };

        $.ajax( { url: '/' + globals.applicationName + '/default/saveConfig',
                  type: "POST",
                  data: changed,
                  success: successHandler } );
    }

    Shadowbox.close();
}

function checkSelectedSaved(divId) {
    var count = $('#' + divId + ' > .selectedSavedClade').length;

    if( count >= 2 ) { $('#graftLink').show(); } else { $('#graftLink').hide(); }
}



